from marshmallow import Schema, fields, validate, validates, ValidationError
from datetime import datetime

class PaymentItemSchema(Schema):
    """支付项目模式"""
    type = fields.Str(required=True, validate=validate.OneOf(['registration', 'prescription', 'medical_record', 'service', 'other']))
    itemId = fields.Str(required=True)
    amount = fields.Float(required=True)

class PaymentSchema(Schema):
    """支付模式"""
    paymentId = fields.Str()
    billingId = fields.Str() 
    patientId = fields.Str(required=True)
    relatedType = fields.Str(validate=validate.OneOf(['appointment', 'prescription', 'medical_record', 'service', 'other']))
    relatedId = fields.Str()
    items = fields.List(fields.Nested(PaymentItemSchema))
    amount = fields.Float(required=True)
    totalAmount = fields.Float()
    paymentMethod = fields.Str(validate=validate.OneOf(['支付宝', '微信', '银行卡', '现金', '医保', '其他']), dump_default='支付宝')
    transactionId = fields.Str()
    status = fields.Str(validate=validate.OneOf(['待支付', '处理中', '成功', '失败', '已取消', '已退款']), dump_default='待支付')
    paidAt = fields.DateTime()
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class PaymentCreateSchema(Schema):
    """创建支付模式"""
    patientId = fields.Str(required=True)
    billingId = fields.Str()
    relatedType = fields.Str(validate=validate.OneOf(['appointment', 'prescription', 'medical_record', 'service', 'other']))
    relatedId = fields.Str()
    items = fields.List(fields.Nested(PaymentItemSchema))
    amount = fields.Float(required=True)
    totalAmount = fields.Float()
    paymentMethod = fields.Str(validate=validate.OneOf(['支付宝', '微信', '银行卡', '现金', '医保', '其他']), dump_default='支付宝')

class PaymentProcessSchema(Schema):
    """处理支付模式"""
    paymentMethod = fields.Str(validate=validate.OneOf(['支付宝', '微信', '银行卡', '现金', '医保', '其他']))
    transactionId = fields.Str()
    status = fields.Str(validate=validate.OneOf(['成功', '失败', '已取消']), required=True)
    
class PaymentQuerySchema(Schema):
    """支付查询模式"""
    patientId = fields.Str()
    billingId = fields.Str()
    status = fields.Str(validate=validate.OneOf(['待支付', '处理中', '成功', '失败', '已取消', '已退款', 'all']))
    paymentMethod = fields.Str(validate=validate.OneOf(['支付宝', '微信', '银行卡', '现金', '医保', '其他', 'all']))
    startDate = fields.Date()
    endDate = fields.Date() 